﻿package src.flip{
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.ui.*;
	public final class Loading extends MovieClip {
		private var i:LoaderInfo;
		private var ld:Sprite = new Sprite();
		private var rg:Sprite = new Sprite();
		private var pt:TextField = new TextField();
		private var co:Number = 0x888888;
		//
		public static var f:TextFormat;
		public function Loading():void {
			stop();
			try {
				addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
			} catch(e:Error) { 
				init(); 
			}
		}
		private function init(e:Event = null):void {
			stage.align = "TL";
			stage.scaleMode = "noScale";
			ring(rg, co, 8, 10);
			pt.autoSize = "center";
			pt.selectable = false;
			//Arial/Verdana
			f = new TextFormat("Arial", 8, co);
			pt.defaultTextFormat = f;
			pt.htmlText = " ";
			pt.x = (- pt.width) * 0.5;
			pt.y = (- pt.height) * 0.5;
			//
			ld.addChild(pt);
			ld.addChild(rg);
			addChild(ld);
			//
			addEventListener(Event.ENTER_FRAME, r, false, 0, true);
			//
			i = root.loaderInfo;
			i.addEventListener(ProgressEvent.PROGRESS, p, false, 0, true);
			i.addEventListener(Event.COMPLETE, c, false, 0, true);
		}
		//ring
		public static function ring(sp:Sprite, color:Number, radius1:Number, radius2:Number):void {
			with (sp.graphics) {
				clear();
				var total:Number = radius2 * 2;
				var trans:Number = 1 / total;
				var degree:Number = 360 / total;
				var radian:Number = degree * Math.PI / 180;
				var cos:Number;
				var sin:Number;
				var tx:Number;
				var ty:Number;
				for (var i:uint = 0; i < total; i ++) {
					beginFill(color, i * trans);
					//
					cos = Math.cos(i * radian);
					sin = Math.sin(i * radian);
					//
					tx = radius1 * cos;
					ty = radius1 * sin;
					moveTo(tx, ty);
					//
					tx = radius2 * cos;
					ty = radius2 * sin;
					lineTo(tx, ty);
					//
					cos = Math.cos((i + 1) * radian);
					sin = Math.sin((i + 1) * radian);
					//
					tx = radius2 * cos;
					ty = radius2 * sin;
					lineTo(tx, ty);
					//
					tx = radius1 * cos;
					ty = radius1 * sin;
					lineTo(tx, ty);
					//
					endFill();
				}
			}
		}
		//rotation
		private function r(e:Event):void {
			rg.rotation += 10;
			ld.x = (stage.stageWidth) * 0.5;
			ld.y = (stage.stageHeight) * 0.5;
		}
		//progress
		private function p(e:ProgressEvent):void {
			pt.htmlText = Math.round((e.bytesLoaded / e.bytesTotal) * 100).toString();;
		}
		//complete
		private function c(e:Event):void {
			removeEventListener(Event.ENTER_FRAME, r);
			removeChild(ld);
			play();
		}
	}
}